import requests
import json

def fanyi(keyword):
    #url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule' #去掉translate后边的_0才可以爬取
    url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
    data = {
        "i":keyword,
        'from':'AUTO',
        'to':'AUTO',
        'smartresult':'dict',
        'client':'fanyideskweb',
        'salt':'15439319935686',
        'sign':'3128d36874d6ee16567b76ebecefb2ac',
        'ts':'1543931993568',
        'bv':'0ef078d6b7f3e6f8047a6cbd85c069cc',
        'doctype':'json',
        'version':'2.1',
        'keyfrom':'fanyi.web',
        'action':'FY_BY_REALTIME',
        'typoResult':'false',
    }
    res = requests.post(url,data=data)
    str_json = res.content.decode('utf-8')
    myjson = json.loads(str_json)
    print(myjson['translateResult'][0][0]['tgt'])
    #print(myjson['translateResult']['tgt'])

if __name__ == '__main__':
    while True:
        keyword = input('请输入要翻译的词：')
        if keyword == 'q':
            break
        fanyi(keyword)


